import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/pages/daily_work_report_module/my_daily_detail_module/my_daily_detail_view.dart';
import 'package:app/pages/daily_work_report_module/my_daily_module/my_daily_controller.dart';
import 'package:app/pages/published_daily_module/published_daily_view.dart';
import 'package:app/router/app_pages.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import '../../../common/app_theme.dart';
import '../../../common/colors.dart';
import '../../../components/nodata_page.dart';
import '../../../services/address.dart';
import '../../../utils/search_module.dart';
import 'my_mission_controller.dart';
import 'my_mission_model.dart';

class MyMissionView extends GetView {

  var type;

  MyMissionView({this.type});


  MyMissionController controller = Get.put(MyMissionController());

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      floatingActionButton: GestureDetector(
        child: Container(
          width: 40,
          height: 40,
          decoration: BoxDecoration(
            color: AppColor.themeColor,
            borderRadius: const BorderRadius.all(Radius.circular(20)),
          ),
          alignment: Alignment.center,
          child: const Text(
            '+',
            style: TextStyle(fontSize: 25, color: Colors.white),
          ),
        ),
        onTap: () async {
          var data =
              await Get.toNamed(AppRoutes.publishedMission, arguments: -1);
          if (data == 'refresh') {
            controller.requestData(reply_type: 'create');
          }
        },
      ),
      appBar:BaseAppBar(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        child: Text('我的任务'),
      ),
      body: Container(
        child: GetBuilder<MyMissionController>(builder: (_) {
          return Column(
            children: [
              Container(
                color: Colors.white,
                height: 45,
                child: Row(
                  children: [
                    Container(
                      margin: EdgeInsets.only(left: 15),
                      child: Row(
                        children: [
                          GestureDetector(
                            child: Text(
                              controller.replyType,
                              style: appThemeData.textTheme.bodyText1!
                                  .copyWith(fontSize: 14),
                            ),
                            onTap: () => controller.topClick(),
                          ),
                          controller.isTopShow == false
                              ? Image.asset(
                                  'images/arrow-down.png',
                                  width: 12,
                                  height: 12,
                                )
                              : Image.asset(
                                  'images/arrow-up.png',
                                  width: 12,
                                  height: 12,
                                  color: AppColor.themeColor,
                                )
                        ],
                      ),
                    ),
                    Container(
                        decoration: BoxDecoration(
                            color: AppColor.bgColor,
                            borderRadius:
                                BorderRadius.all(Radius.circular(13))),
                        width: Get.width - 150,
                        alignment: Alignment.centerLeft,
                        margin: EdgeInsets.only(top: 0, left: 10, right: 10),
                        padding: EdgeInsets.only(left: 15),
                        height: 30,
                        child: Row(
                          children: [
                            Icon(
                              Icons.search,
                              size: 20,
                              color: AppColor.smallTextColor,
                            ),
                            Container(
                              alignment: Alignment.center,
                              color: Colors.transparent,
                              width: Get.width - 200,
                              height: 30,
                              child: TextField(
                                controller: controller.editingController,
                                onChanged: (values) {
                                  // requestData(search: values);
                                },
                                decoration: InputDecoration(
                                    isCollapsed: true,
                                    border: InputBorder.none,
                                    hintText: '请输入关键词',
                                    hintStyle: TextStyle(
                                        color: AppColor.hintColor,
                                        fontSize: 13)),
                                style: TextStyle(
                                    color: AppColor.smallTextColor,
                                    fontSize: 14),
                              ),
                            )
                          ],
                        )),
                  ],
                ),
              ),
              Expanded(
                  child: Container(
                padding: EdgeInsets.only(top: 5),
                color: AppColor.bgColor,
                width: Get.width,
                child: GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    /// 页面点击响应
                    SearchModule.closeKeyboard(context);
                  },
                  onPanDown: (_) {
                    /// 列表滑动响应
                    SearchModule.closeKeyboard(context);
                  },
                  child: EasyRefresh.custom(
                    controller: controller.easyRefreshController,
                    footer: MaterialFooter(),
                    emptyWidget: controller.dataArr.isNotEmpty
                        ? null
                        : Container(
                            height: Get.height,
                            color: Colors.red,
                            child: NoDataPage(),
                          ),
                    onRefresh: () => controller.pullToData(),
                    onLoad: () => controller.onLoadToData(),
                    slivers: [
                      SliverList(
                        delegate: _mySliverChildBuilderDelegate(),
                      ),
                    ],
                  ),
                ),
              )),
            ],
          );
        }),
      ),
    );
  }

  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
      (BuildContext context, int index) {
        MyTaskItemModel _model = controller.dataArr[index];
        return GestureDetector(
          onTap: () {

            if(type == 'relevancy'){
              var params  = {
                'name':_model.name,
                'id':_model.id,
              };
              Get.back(result:params);
              return;
            }

            controller.pushToView(index);
          },
          child: Container(
            // margin: EdgeInsets.all(5),
            // height: 10,
            margin: EdgeInsets.only(top: 10),
            padding: EdgeInsets.all(10),
            color: Colors.white,
            child: Row(
              children: [
                const Image(
                  image: AssetImage("images/mine_icons3.png"),
                  width: 36,
                  height: 36,
                ),
                Container(
                  margin: const EdgeInsets.only(
                    left: 12,
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        margin: const EdgeInsets.only(top: 4),
                        color: Colors.white,
                        width: Get.width - 80,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Container(
                              color: Colors.white,
                              width: Get.width-200,
                              child: Text(
                                '${_model.name}',
                                style: appThemeData.textTheme.bodyText1,
                              ),
                            ),
                            Text('状态:${_model.state }')
                          ],
                        ),
                      ),
                      const SizedBox(
                        height: 3,
                      ),

                      Text(
                        '${_model.planDateBegin!.length > 10 ? _model.planDateBegin!.substring(0, 10) : _model.planDateBegin}',
                        style: appThemeData.textTheme.subtitle1
                            ?.copyWith(fontSize: 11),
                      ),
                      const SizedBox(
                        height: 3,
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        );
      },
      childCount: controller.dataArr.length,
    );
  }
}
